﻿using UnityEngine;
using UnityEngine.UI;

public class game : UnitySingleton<game> {

	public enum Game_Status
	{
		Ready,
		GameIn,
		GameOver,
	};
	private Game_Status status;
	public Game_Status Status
	{
		get{return status;}
		set
		{
			status=value;
			UIChange();
		}
	}

	public GameObject PannelStart;
	public GameObject PannelGameIn;
	public GameObject PannelGameOver;


	public Text uiGamescore;
	private int score = 0;
    public Text uiGameOverscore;
    public Text uibest;
    private Player player;

    // Use this for initialization
    void Start () {
        player = GameObject.Find("player").GetComponent<Player>();
		Status= Game_Status.Ready;
        event_manager.Instance.add_listener("score", OnScore);
        event_manager.Instance.add_listener("best", OnBest);
	}

    void OnScore(object udata)
    {
        score += (int)udata;
        if(PannelGameIn)
            uiGamescore.text = "score:"+score.ToString();
    }

    void OnBest(object udata)
    {
        if (!PannelGameOver)
            return;
        uiGameOverscore.text = score.ToString();
        int best = PlayerPrefs.GetInt("best", 0);
        if(best<score)
        {
            best = score;
            PlayerPrefs.SetInt("best", best);
        }
        uibest.text = best.ToString();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
            Application.Quit();
    }

	public void GameStart()
	{
		Status= Game_Status.GameIn;
		player.Fly ();
		PipeLinesManager.Instance.StartPipe ();
	}

	public void Restart()
	{
        UnityEngine.SceneManagement.SceneManager.LoadScene(0);
    }

	public void UIChange()
	{
		PannelStart.SetActive (Status == Game_Status.Ready);
		PannelGameIn.SetActive (Status == Game_Status.GameIn);
		PannelGameOver.SetActive (Status == Game_Status.GameOver);
    }
}
